// pages/dingdan/dingdan.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderList: [],
    userInfo: {},
    imgBaseUrl: app.globalData.imgBaseUrl,
    cancelReason:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  cancleOrder(event) {
    let that = this;
    console.log("cancleEvent",event);
    if (event.currentTarget.dataset.item.status !== "预约中") {
      wx.showToast({
        title: `该订单已经${event.currentTarget.dataset.item.status}`,
        icon: "none",
        duration: 2000,
      });
    } else {
      wx.showModal({
        title: '提示',
        content: '是否取消该订单？',
        success(res) {
          if (res.confirm) {
            wx.request({
              url: app.globalData.baseUrl + "/orders/" + event.currentTarget.dataset.orderid,
              header: {
                "cookie": "JESSIONID=" + wx.getStorageSync("userInfo").sessionId
              },
              method: "delete",
              success(res) {
                console.log(res)
                if (res.data.code == 1) {
                  wx.showToast({
                    title: '取消成功',
                    icon: "success",
                    duration: 1500,
                    success() {
                      wx.request({
                        url: app.globalData.baseUrl + "/orders/" + event.currentTarget.dataset.orderid,
                        header: {
                          "cookie": "JESSIONID=" + wx.getStorageSync("userInfo").sessionId
                        },
                        method: "DELETE",
                        success(res) {
                          console.log("deleteRes",res);
                          wx.reLaunch({
                            url: "/pages/shouye/shouye",
                          });
                        },
                        fail(err) {
                          console.log("deleteErr", err);
                        }
                      });
                    }
                  })
                } else {
                  wx.showToast({
                    title: '服务器异常！',
                    icon: "none",
                    duration: 2000,
                  });
                }
              },
              fail(err) {
                wx.showToast({
                  title: '服务器异常！',
                  icon: "none",
                  duration: 2000,
                });
                console.log("cancleOrderErr", err);
              }
            })
          } else if (res.cancel) {
            return;
          }
        }
      });
    }
   
  },
  onLoad: function(options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    /**
     * 获取个人订单
     */
    if (wx.getStorageSync("userInfo")) {
      wx.request({
        url: app.globalData.baseUrl + "/orders/" + wx.getStorageSync("userInfo").userId,
        method: "GET",
        header: {
          "cookie": "JESSIONID=" + wx.getStorageSync("userInfo").sessionId
        },
        success(res) {
          let orderList = res.data;
          //遍历订单结果，获取货物总数和货物总价格
          orderList.forEach((orderVal, orderIndex, order_arr) => {
            order_arr[orderIndex].amountPrice = 0;
            order_arr[orderIndex].amountNum = 0;
            orderVal.extendGoods.forEach((goodsVal, goodsIndex, goodsList) => {
              order_arr[orderIndex].amountNum += goodsVal.num;
            });
          });
          that.setData({
            orderList: orderList
          });
        },
        fail(err) {
          console.log("getOrdersByIdErr", err);
        }
      });
    } else {
      wx.showToast({
        title: '请先登录！',
        icon: "loading",
        duration: 3000,
        success: function () {
          wx.switchTab({
            url: '/pages/huiyuan/huiyuan'
          });
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})